package com.ccys.app.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.ccys.app.service.SubscriptionActivationCodeScreenService;
import com.ccys.common.vo.ResponseResultApp;
import com.ccys.db.cache.UserUtil;
import com.ccys.db.dto.app.subscriptionActivationCodeScreen.ActivateSubscriptionDto;
import com.ccys.db.dto.app.subscriptionActivationCodeScreen.DeleteConsultProfileDto;
import com.ccys.db.dto.app.subscriptionActivationCodeScreen.DeleteDependantSubscriptionDto;
import com.ccys.db.entity.*;
import com.ccys.db.mapper.TblCountryServiceTypeProfileTypeMapper;
import com.ccys.db.mapper.TblPlanBenefitIdentifierMapper;
import com.ccys.db.mapper.TblPlanBenefitMemberMapper;
import com.ccys.db.mapper.TblPlanMapper;
import com.ccys.db.vo.app.subscriptionAddnewScreen.GetConsultProfileTypeMakeVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: WhiteCoat
 * @description: 激活订阅的接口
 * @author: ZhangJingCan
 * @create: 2024-01-30 16:59
 **/

@Service
public class SubscriptionActivationCodeScreenServiceImpl implements SubscriptionActivationCodeScreenService {

    @Resource
    private TblPlanBenefitIdentifierMapper tblPlanBenefitIdentifierMapper;

    @Resource
    private TblPlanBenefitMemberMapper tblPlanBenefitMemberMapper;

    @Resource
    private TblPlanMapper tblPlanMapper;

    @Resource
    private TblCountryServiceTypeProfileTypeMapper tblCountryServiceTypeProfileTypeMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResultApp activateSubscription(ActivateSubscriptionDto dto) {
        //校验是否登录
        TblMember member = UserUtil.getCurLoginMember();
        if (ObjectUtil.isEmpty(member)) {
            return ResponseResultApp.error(401, "Request require authorization.");
        }

        if (ObjectUtil.isEmpty(dto.getActive_code())) {
            return ResponseResultApp.error(400, "The active_code field is required.");
        }

        if (ObjectUtil.isEmpty(dto.getProfile_type_id())) {
            return ResponseResultApp.error(400, "The profile_type_id field is required.");
        }


        //先检查是否存在这个订阅数据
        Example identifierExample = new Example(TblPlanBenefitIdentifier.class);
        identifierExample.createCriteria().andEqualTo("identifier", dto.getActive_code()).andEqualTo("delFlag", 0);
        TblPlanBenefitIdentifier identifier = tblPlanBenefitIdentifierMapper.selectOneByExample(identifierExample);
        if (ObjectUtil.isEmpty(identifier)) {
            return ResponseResultApp.error(400, "Subscription code not found");
        }

        //准备数据 - 添加数据
        TblPlanBenefitMember benefitMember = new TblPlanBenefitMember();
        benefitMember.setMemId(member.getId());
        benefitMember.setBenefitId(identifier.getBenefitId());
//      todo  identifier字段已经删除

//        benefitMember.setIdentifier(identifier.getIdentifier());
        benefitMember.setIdentifierInputtedByPatient(dto.getActive_code());
        benefitMember.setDependantInputtedBy(member.getId());
        benefitMember.setActivated(1);
        benefitMember.setStatus(1);
        //保存数据库
        tblPlanBenefitMemberMapper.insertSelective(benefitMember);
//        benefitMember.setCorrelationid();
        //找plan的name - 也就是对方返回的packageName
        TblPlan tblPlan = tblPlanMapper.selectByPrimaryKey(identifier.getBenefitId());
        String str = "";
        if (ObjectUtil.isNotEmpty(tblPlan.getName())) {
            str = tblPlan.getName();
        }

        return ResponseResultApp.success("Account activation subscription:" + str, null);
    }

    @Override
    public ResponseResultApp<List<GetConsultProfileTypeMakeVo>> getConsultProfileType(Integer serviceType) {
        //校验是否登录
        TblMember member = UserUtil.getCurLoginMember();
        if (ObjectUtil.isEmpty(serviceType)) {
            return ResponseResultApp.error(400, "The service_type field is required.");
        }
        //查询并返回
        List<GetConsultProfileTypeMakeVo> vos = tblCountryServiceTypeProfileTypeMapper.memberFindListByCountryId(member.getCurrentCountry());
        return ResponseResultApp.success(vos);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResultApp deleteDependantSubscription(DeleteDependantSubscriptionDto dto) {
        if (ObjectUtil.isEmpty(dto.getProfile_id())) {
            return ResponseResultApp.error(400, "The profile_id field is required.");
        }
        if (ObjectUtil.isEmpty(dto.getDependant_id())) {
            return ResponseResultApp.error(400, "The dependant_id field is required.");
        }
        if (ObjectUtil.isEmpty(dto.getActive_code())) {
            return ResponseResultApp.error(400, "The active_code field is required.");
        }
        tblPlanBenefitMemberMapper.deleteDependantSubscription(dto);
        return ResponseResultApp.success("Success", true);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResultApp deleteConsultProfile(DeleteConsultProfileDto dto) {
        //检查如果传入了childId则代表删除小孩的
        if (ObjectUtil.isEmpty(dto.getId())) {
            return ResponseResultApp.error(400, "The id field is required.");
        }
        String memberId = "";
        if (ObjectUtil.isNotEmpty(dto.getChild_id())) {
            memberId = dto.getChild_id();
        } else {
            memberId = UserUtil.getCurLoginMember().getId();
        }
        //软删除
        tblPlanBenefitMemberMapper.deleteConsultProfile(memberId, dto.getId());
        return ResponseResultApp.success("Success", "");
    }
}
